/**
 * 
 */
package com.estock.common.model.security;

import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.estock.common.model.security.enums.StockType;

/**
 * @author richard
 *
 */
@Entity
@Table(name="Index")
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class Index extends BaseEntity {

	private static final long serialVersionUID = -2259146919534801703L;
	
	@OneToOne(mappedBy="index", cascade=CascadeType.ALL)
	@JsonIgnore
	private IndexPrice indexPrice = new IndexPrice();
	
	private String code;
	
	private int type;
	
	private String name;
	
	public Index(){
	}
	
	public Index(String code, int type){
		this.code = code;
		this.type = type;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public IndexPrice getIndexPrice() {
		// fix indexPrice null primary key issue.
		if (indexPrice.getIndex() == null) {
			indexPrice.setIndex(this);
		}
		return indexPrice;
	}

	public void setIndexPrice(IndexPrice indexPrice) {
		this.indexPrice = indexPrice;
	}
	
	public void setYesterPrice(float yesterPrice) {
		indexPrice.setYesterPrice(yesterPrice);
	}
	
	public void setLatestPrice(float latestPrice) {
		indexPrice.setLatestPrice(latestPrice);
	}

	@Transient
	public String getTypeName() {
		return StockType.getDescByStockType(getType());
	}
	
	@Transient
	public float getYesterPrice() {
		return indexPrice.getYesterPrice();
	}

	@Transient
	public float getLatestPrice() {
		return indexPrice.getLatestPrice();
	}
	
	@Transient
	public Date getLastTransDay() {
		return indexPrice.getLastTransDay();
	}

}
